home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / z150_src.lzh / ZOOPACK.C < prev   
Encoding:
C/C++ Source or Header  |  1987-07-12  |  11.6 KB  |  356 lines

  1. #ifndef LINT
  2. /* @(#) zoopack.c 1.12 87/05/29 12:56:31 */
  3. static char sccsid[]="@(#) zoopack.c 1.12 87/05/29 12:56:31";
  4. #endif /* LINT */
  5.  
  6. /*
  7. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  8. */
  9. #include "options.h"
  10. /* Packs an archive.  The sequence is:
  11.    1. Copy all files from current archive to new one.
  12.    2. If the user didn't want a backup, delete the old archive
  13.       else rename it to same name with extension of .BAK.
  14.    3. Rename temporary archive to old name.
  15. */
  16.  
  17. /* define this to make packing noisless except for dots */
  18. #define QUIETPACK 1
  19.  
  20.  
  21. #include "portable.h"
  22. #include <stdio.h>
  23. #include "various.h"
  24. #include "zoo.h"
  25. #include "zoofns.h"
  26. #include "errors.i"
  27. #ifndef NOSIGNAL
  28. #include <signal.h>
  29. #endif
  30.  
  31. #ifdef FLAT
  32. #include <types.h>
  33. #include <stat.h>
  34. #else
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #endif
  38.  
  39. #ifdef NOFCNTL
  40. #include <file.h>
  41. #else
  42. #include <fcntl.h>
  43. #endif
  44.  
  45. struct zoo_header zoo_header = {
  46.    TEXT,
  47.    ZOO_TAG,
  48.    (long) SIZ_ZOOH,
  49.    (long) (-SIZ_ZOOH),
  50.    MAJOR_VER,
  51.    MINOR_VER
  52. };
  53. char file_leader[] = FILE_LEADER;
  54. extern int quiet;
  55. int break_hit;
  56.  
  57. #ifdef LINT_ARGS
  58. int ver_too_high (struct zoo_header *);
  59. #else
  60. int ver_too_high();
  61. #endif
  62.  
  63. void zoopack(zoo_path, option)
  64. char *zoo_path, *option;
  65. {
  66. static char temp_file[PATHSIZE] = "XXXXXX";
  67. #ifndef NOSIGNAL
  68. int (*oldsignal)();
  69. #endif
  70. register int zoo_han;                     /* handle for open archive */
  71. register FILE *zoo_file;                  /* stream for open archive */
  72. int new_han;                              /* handle of destination archive */
  73. long next_ptr;                            /* pointer to within archive */
  74. long new_dir_pos;                         /* ditto */
  75. struct direntry direntry;                 /* directory entry */
  76. struct zoo_header old_zoo_header;         /* just for reading old header */
  77. int status;                               /* error status */
  78. int nobackup = 0;                         /* keep backup */
  79. int force = 0;                            /* force overwrite of old backup */
  80. int extcount = 0;                         /* how many files moved */
  81. char backup_name[PATHSIZE];               /* name of backup */
  82. int bad_header = 0;                       /* if archive has bad header */
  83. int latest_date = 0;                      /* latest date on any file moved */
  84. int latest_time = 0;                      /*  ...likewise */
  85. int curr_dir = 0;                                    /* create backup in curr dir */
  86. static char partial_msg[] =
  87.    "Partially packed archive left in %s.\n";
  88.  
  89. while (*option) {
  90.    switch (*option) {
  91.       case 'P': force++; break;
  92.       case 'E': nobackup++; break;
  93.       case 'q': quiet++; break;
  94.         case '.': curr_dir++; break;
  95.       default:
  96.          prterror ('f', inv_option, *option);
  97.    }
  98.    option++;
  99. }
  100. if (force == 1)         /* force only if P was doubled */
  101.    force--;
  102.  
  103. zoo_path = addext (zoo_path, EXT_DFLT);      /* add default extension */
  104.  
  105. /* Create a backup name by replacing any extension by backup extension. */
  106. strcpy (backup_name, zoo_path);
  107. {
  108.    char *temp;
  109.    if (temp = strrchr (backup_name,EXT_CH))      /* if dot found */
  110.       strcpy (temp, BACKUP_EXT);                /* replace old extension */
  111.    else
  112.       strcat (backup_name, BACKUP_EXT);         /* else just append */
  113. }
  114.  
  115. /* Open original archive for read-only access */
  116. zoo_file = fopen(zoo_path, FRDSTR);
  117. zoo_han = OPEN(zoo_path, F_READ);    /* different open */
  118.  
  119. if (zoo_file == NULL || zoo_han == -1)
  120.    prterror ('f', could_not_open, zoo_path);
  121.  
  122. /* Read the header of the old archive. */
  123. frd_zooh(&old_zoo_header, zoo_file);
  124. /* fread ((char *) &old_zoo_header, sizeof(old_zoo_header), 1, zoo_file); */
  125.  
  126. if ((old_zoo_header.zoo_start + old_zoo_header.zoo_minus) != 0L) {
  127.    prterror ('w', failed_consistency);
  128.    ++bad_header;                    /* remember for future error message */
  129. }
  130.  
  131. /* Refuse to pack it if its version number is higher than we can accept */
  132. if (ver_too_high (&old_zoo_header))
  133.    prterror ('f', wrong_version, old_zoo_header.major_ver,
  134.                                     old_zoo_header.minor_ver);
  135.  
  136. fseek (zoo_file, old_zoo_header.zoo_start, 0); /* seek to where data begins */
  137.  
  138. /* Now see if the archive already exists with the backup extension.  If so,
  139.    give an error message and abort.  However, we skip this test if the user
  140.    specified overwriting the backup */
  141.  
  142. if (!force) {
  143.    if (exists (backup_name))
  144.       prterror ('f', "File %s already exists.  Delete it or use PP option.\n",
  145.                       backup_name);
  146. }
  147.  
  148. /*
  149. Open the new archive by a temporary name.  If not otherwise specified,
  150. we open the new archive in the same directory as the original.  But if
  151. the curr_dir switch was given, we leave temp_file unchanged, and it
  152. was already initialized to be just the template XXXXXX.
  153. */
  154. if (!curr_dir) {
  155.     strcpy (temp_file, zoo_path);          /* original archive name */
  156.     *nameptr (temp_file) = '\0';           /* ... minus original filename */
  157.     strcat (temp_file, "XXXXXX");          /* ... plus XXXXXX */
  158. }
  159. mktemp (temp_file);                    /* ... and make unique */
  160.  
  161. new_han = CREATE(temp_file, F_RDWR);
  162. if (new_han == -1)
  163.    prterror ('f', "Could not create temporary file %s.\n", temp_file);
  164.  
  165. /* Write the header of the new archive, updated with our own data */
  166. wr_zooh (&zoo_header, new_han);
  167. /* write (new_han, (char *) &zoo_header, sizeof(zoo_header)); */
  168. lseek (new_han, zoo_header.zoo_start, 0);       /* position to add files */
  169.  
  170. /* Now we loop through the old archive's files and add each to the new
  171. archive.  The only changes needed are to update the .next and .offset
  172. fields of the directory entry. */
  173.  
  174. while (1) {
  175.  
  176.    frd_dir(&direntry, zoo_file);
  177. /* fread ((char *) &direntry, sizeof (direntry), 1, zoo_file); */
  178.  
  179.    if (direntry.zoo_tag != ZOO_TAG) {
  180.       long currpos, zoolength;
  181.       prterror ('F', bad_directory);
  182.       if (bad_header) {    /* bad headers means don't save temp file */
  183.          close (new_han);
  184.          unlink (temp_file);
  185.       } else {
  186.          writenull (new_han, MAXDIRSIZE);    /* write final null entry */
  187.          printf (partial_msg, temp_file);
  188.          if ((currpos = ftell (zoo_file)) != -1L)
  189.             if (fseek (zoo_file, 0L, 2) == 0)
  190.                if ((zoolength = ftell (zoo_file)) != -1L)
  191.                   printf (cant_process, zoolength - currpos);
  192.       }
  193.       exit (1);
  194.    }
  195.    if (direntry.next == 0L) {                /* END OF CHAIN */
  196.       break;                                 /* EXIT on end of chain */
  197.    }
  198.    next_ptr = direntry.next;                 /* ptr to next dir entry */
  199.  
  200.    if (!direntry.deleted) {
  201. #ifdef QUIETPACK
  202. /* nothing */
  203. #else
  204.       prterror ('m', "%-14s -- ",
  205.          direntry.namlen > 0 ? direntry.lfname : direntry.fname);
  206. #endif
  207.  
  208.       if (lseek (zoo_han, direntry.offset, 0) == -1L) {
  209.          prterror ('f', "Could not seek to file data.\n");
  210.       } else {
  211.          extcount++;          /* update count of files extracted */
  212.  
  213.          /* write a directory entry for this file */
  214.          new_dir_pos = tell (new_han);   /* new direntry pos in new archive */
  215.  
  216.          /*
  217.          Write a null directory entry to preserve integrity in case of
  218.          program being interrupted.  Note:  I don't think it is
  219.          necessary to save direntry.next but I haven't checked.
  220.  
  221.          Old code was:
  222.          writenull(new_han, SIZ_DIRL + direntry.var_dir_len);
  223.          */
  224.          {
  225.             long oldnext;
  226.             oldnext = direntry.next;
  227.             direntry.next = 0L;
  228.             wr_dir(&direntry, new_han);
  229.             direntry.next = oldnext;
  230.          }
  231.  
  232.          lseek (zoo_han, direntry.offset, 0);     /* where to start copying */
  233.          /* Write file leader and remember position of new file data */
  234.          write (new_han, file_leader, SIZ_FLDR);
  235.          direntry.offset = tell(new_han);
  236.          status = getfile (zoo_han, new_han, direntry.size_now, 0);
  237.          /* if no error copy any comment attached to file */
  238.          if (status == 0 && direntry.cmt_size != 0) {
  239.             lseek (zoo_han, direntry.comment, 0);  /* seek to old comment  */
  240.             direntry.comment = tell (new_han);  /* location of new comment */
  241.             status = getfile (zoo_han, new_han,
  242.                                  (long) direntry.cmt_size, 0);
  243.          }
  244.  
  245.          if (status != 0) {
  246.             if (status == 1) {
  247.                memerr();
  248.             } else
  249.                if (status == 2 || status == 3) {
  250.                   prterror ('F', disk_full);
  251.                   printf (partial_msg, temp_file);
  252.                   exit (1);
  253.                } else
  254.                   prterror ('f', internal_error);
  255.          } else {
  256.             if (latest_date < direntry.date ||
  257.                      (latest_date == direntry.date &&
  258.                            latest_time < direntry.time))  {
  259.                latest_date = direntry.date;
  260.                latest_time = direntry.time;
  261.             }
  262.          }
  263.          direntry.next = tell (new_han);
  264.          lseek (new_han, new_dir_pos, 0);    /* position to write direntry */
  265.  
  266.          break_hit = 0;
  267. #ifndef NOSIGNAL
  268.          oldsignal = signal (SIGINT, SIG_IGN);
  269.          if (oldsignal != SIG_IGN)
  270.             signal (SIGINT, handle_break);
  271. #endif
  272.  
  273.          if (wr_dir (&direntry, new_han) != -1 &&
  274.             write (new_han, file_leader, SIZ_FLDR) != -1) {
  275. #ifdef QUIETPACK
  276.             /* prterror ('M', "."); */ ;
  277. #else
  278.             prterror ('M', "moved\n");
  279. #endif
  280.          } else
  281.             prterror ('f', "Write to temporary packed archive %s failed.\n", temp_file);
  282. #ifndef NOSIGNAL
  283.          signal (SIGINT, oldsignal);
  284. #endif
  285.          if (break_hit)
  286.             exit (1);
  287.  
  288.          lseek (new_han, direntry.next, 0);  /* back to end of new archive */
  289.       }  /* end if (lseek ... */
  290.    } /* end if (!direntry.deleted) */
  291.  
  292. fseek (zoo_file, next_ptr, 0);   /* ..seek to next dir entry */
  293. } /* end while */
  294.  
  295. fclose (zoo_file);
  296.  
  297. /* write a final null entry */
  298. writenull (new_han, MAXDIRSIZE);
  299.  
  300. #ifdef NIXTIME
  301. close (new_han);
  302. setutime (temp_file, latest_date, latest_time);
  303. #else
  304. settime (new_han, latest_date, latest_time);    /* adjust its time */
  305. close (new_han);
  306. #endif
  307.  
  308. /* Important note:  At this point, it is assumed that the archive was
  309.    packed and written to a new file without error.  If control reaches
  310.    here without the new archive having been written properly, then
  311.    loss of data due to deletion of the original file could occur.  In
  312.    other words, if something went wrong, execution MUST NOT reach here. */
  313.  
  314. if (extcount == 0) {
  315.    unlink (temp_file);
  316.    prterror ('m', "No files moved.\n");
  317. } else {
  318.    char new_name[FNAMESIZE];
  319.    /* (a) if user requested, delete original, else rename it to
  320.    *.bak.  (b) rename temp file to same base name as zoo_file. */
  321.  
  322. #ifdef QUIETPACK
  323.    /* prterror('M', "\n"); */
  324. #endif
  325.  
  326.    unlink (backup_name);    /* remove any previous backup in any case */
  327.    if (nobackup)
  328.       unlink (zoo_path);
  329.    else
  330.       chname (backup_name, zoo_path);
  331.  
  332.     /* if we are packing into current directory, we will rename temp file
  333.         to same basename but without the preceding pathname */
  334.     if (curr_dir)
  335.         zoo_path = nameptr (zoo_path);        /* strip pathname */
  336.  
  337.    if (chname (zoo_path, temp_file)) {
  338.       prterror ('w', "Renaming error.  Packed archive is now in %s.\n", temp_file);
  339.       exit (1);
  340.    }
  341. } /* end if */
  342.  
  343. } /* end zoopack() */
  344.  
  345.  
  346. /* handle_break() */
  347. /* Sets break_hit to 1 when called */
  348. int handle_break()
  349. {
  350. #ifndef NOSIGNAL
  351.    signal (SIGINT, SIG_IGN);     /* ignore future control ^Cs for now */
  352.    break_hit = 1;
  353. #endif
  354. }
  355.  
  356.